package com.admin.education.models.Mapper;

import com.admin.education.models.*;
import org.mapstruct.*;
import org.mapstruct.factory.Mappers;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@Mapper
public abstract class UserAnswerSheetModelMapper {
    @Mappings({
            @Mapping(target = "id", ignore = true),
            @Mapping(target = "userAnswerSheetAnswers", ignore = true),
            @Mapping(target = "userAnswerSheet", ignore = true),
            @Mapping(target = "userAnswerSheetOwns", ignore = true)
    })
    public abstract UserAnswerSheetQuestion mapFrom(TemplateQuestions templateQuestions);

    @Mappings({})
    public abstract List<UserAnswerSheetQuestion> mapFrom(List<TemplateQuestions> templateQuestions);

    @Mappings({
            @Mapping(target = "id", ignore = true),
            @Mapping(target = "userAnswerSheetQuestion", ignore = true)
    })
    public abstract UserAnswerSheetAnswer mapFrom(TemplateAnswers templateAnswers);

    @Mappings({})
    public abstract List<UserAnswerSheetAnswer> mapFromTemplateAnswers(List<TemplateAnswers> templateAnswers);

    @AfterMapping
    public void afterMapping(TemplateQuestions templateQuestions, @MappingTarget UserAnswerSheetQuestion userAnswerSheetQuestion) {

        if (null != templateQuestions.getTemplateAnswers()) {
            userAnswerSheetQuestion.setUserAnswerSheetAnswers(this.mapFromTemplateAnswers(templateQuestions.getTemplateAnswers()));

            for (UserAnswerSheetAnswer userAnswerSheetAnswer : userAnswerSheetQuestion.getUserAnswerSheetAnswers()) {
                userAnswerSheetAnswer.setUserAnswerSheetQuestion(userAnswerSheetQuestion);
            }
        } else
            userAnswerSheetQuestion.setUserAnswerSheetAnswers(new ArrayList<>());
        userAnswerSheetQuestion.setUserAnswerSheetOwns(new ArrayList<>());
    }
}
